<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

define('ROOT_PATH',dirname(__FILE__)."/");

require_once(ROOT_PATH."configs/config.php");
require_once(ROOT_PATH."includes/log.php");

require(ROOT_PATH."includes/lang.php");

require_once(ROOT_PATH."includes/util.func.php");
require_once(ROOT_PATH."func/functions.inc.php");

require_once(ROOT_PATH."class/template.class.php");
require_once(ROOT_PATH."class/dbmanager.class.php");
require_once(ROOT_PATH."class/loader.class.php");

require_once(ROOT_PATH."class/user.class.php");

$conffile = ROOT_PATH."configs/dbconf.php";
$sqlfile = ROOT_PATH."sql/tables.sql";

$tm = new Smarty;
$tm->assign('lang',$LANG);

$param=util::fillparam();

$complete=false;

if ($param['action']=='setup') {
	
	$tm->assign('param',$param);
	
	if (isEmpty($param['dbusername'])
		or
		isEmpty($param['dbpassword'])
		or
		isEmpty($param['dbhost'])
		or
		isEmpty($param['dbname'])
		or
		isEmpty($param['adminuser'])
		or
		isEmpty($param['adminpass'])
		or
		isEmpty($param['adminpass2'])
		or
		isEmpty($param['adminmail'])
		) {
			$error = $LANG['regmissing'];
		} elseif ($param['adminpass']!=$param['adminpass2']) {
			$error = $LANG['passnotmatch'];
		} elseif (!isValidEmail($param['adminmail'])) {
			$error = $LANG['invalidmail'];
		} elseif (strlen($param['adminuser'])<MIN_USER_LEN) {
			$error = $LANG['useridtooshort'];
		} elseif (strlen($param['adminpass'])<MIN_PASSWORD_LEN) {
			$error = $LANG['passtooshort'];
		} else {
			// CHECK DB CONNECTION
			$dbuser = $param['dbusername'];
			$dbpass = $param['dbpassword'];
			$dbhost = $param['dbhost'];
			$dbname = $param['dbname'];
			
			$db = new dbmanager($dbhost,$dbuser,$dbpass);
			$test = $db->testConnect($dbname);
			
			if ($test) {

				$f = fopen($conffile, 'w');
				$sql = fopen($sqlfile, 'r');

				if (!$f or !$sql) {
					$error = $LANG['setupfailfile'];
				} else {
					$filedata = "<?php\n";
					$filedata .= "define('DB_USER','".$dbuser."');\n";
					$filedata .= "define('DB_PASS','".$dbpass."');\n";
					$filedata .= "define('DB_HOST','".$dbhost."');\n";
					$filedata .= "define('DB_NAME','".$dbname."');\n";
					$filedata .= "?>";

					fclose($sql);
					$query = file($sqlfile);

					//$query=fread($sql,filesize($sqlfile));
					//fclose($sql);
					
					$db->connect($dbname);
					$loader = new loader($db);				

					//$db->execute($query);		
					$fail = false;
					foreach ($query as $line => $squery) {
						if (trim($squery) != '') {
	    					$res = $db->execute($squery);
    						if (!$res) {
    							$error =$LANG['setupfailquery']." [".$squery."] ".$db->error;
    							echo $error."<br>";
    							$fail=true;
    							break;
    						}
						}
					}
					
					if (!$fail) {
						fwrite($f, $filedata);
						fclose($f);
		
						$us = new user;
						$us->userid=$param['adminuser'];
						$us->mail=$param['adminmail'];
						$us->password=$param['adminpass'];
						$us->groupid=GROUP_ADMIN;		
						$us->status=STATUS_ACTIVE;
						$ret = $loader->save($us);
						$complete = true;
						$message=$LANG['setupcomplete'];
						$content = "&nbsp;";	
						
						include(ROOT_PATH."defsetup/def.php");
					}
					
					$db->close();

					unset($loader);
					unset($db);				
				}
				
			} else {
				$error =$db->error;
			}
			
		}		
}

if (!$complete) {
	$tm->assign('error',$error);		
	$content = $tm->fetch('setup.tpl');
}

$tm->assign('message',$message);
$tm->assign('error',$error);		
$tm->assign('content',$content);
$tm->display('index.tpl');

?>
